package com.mt.fpb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.mapper.EarningDetailMapper;
import com.mt.fpb.mapper.WxProductMapper;
import com.mt.fpb.mapper.WxProductOrderMapper;
import com.mt.fpb.model.EarningDetail;
import com.mt.fpb.model.WxProduct;
import com.mt.fpb.model.WxProductOrder;
import com.mt.fpb.model.dto.MoneyInfo;
import com.mt.fpb.model.vo.CommonResult;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;



@Controller
@Slf4j
@RequestMapping("/Order")
public class ProductOrderController {
    @Autowired
    private WxProductMapper productMapper;

    @Autowired
    private WxProductOrderMapper productOrderMapper;
    @Autowired
    private EarningDetailMapper earningDetailMapper;

    /**
     * 产品明细
     * @param productName
     * @return
     */
    @ResponseBody
    @GetMapping("/productInfo")
    public CommonResult productInfo(String productName){
        WxProduct product=new WxProduct();
        product=productMapper.getProductByProductName(productName);
        if(product!=null)
        return CommonResult.success(product);
        else
        return  CommonResult.fail(500,"没有该数据");
    }


    /**
     * 生成订单
     * @param employeeId
     * @param productId
     * @param payMoney
     * @return
     */
    @ResponseBody
    @GetMapping("productOrder")
    public CommonResult generatedOrder(String employeeId,String productId,float payMoney){

        WxProductOrder productOrder=new WxProductOrder();
        //获取当前时间
        Date  date=new Date();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("product_id",productId);
        WxProduct product=productMapper.selectOne(queryWrapper);
        long  lockTime=product.getLockTime();
//        long payTimes= System.currentTimeMillis();
        //获取第二天0点的时间
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);

        Date secondtime=calendar.getTime();
        System.out.println("第二天的时间为:"+secondtime);
        //获取第二天0点的时间戳
        long payTimes=secondtime.getTime();
        long timps=payTimes+(lockTime*24*60*60*1000);
        //结束时间
        Date endTime=new Date(timps);
        System.out.println("结束时间为:"+endTime);
        productOrder.setId(UUID.randomUUID().toString().replace("-","").toLowerCase());
        productOrder.setEffectiveTime(secondtime);
        productOrder.setOrderStatus("0");
        productOrder.setStartTime(date);
        productOrder.setPayMoney(payMoney);
        productOrder.setOrderMoney(payMoney);
        productOrder.setEmployeeId(employeeId);
        productOrder.setProductId(productId);
        productOrder.setEndTime(endTime);
        int i= productOrderMapper.insert(productOrder);
        if(i==1){
            return  CommonResult.success(productOrder);
        }
        else {
            return CommonResult.fail(405,"生成订单失败");
        }
    }


    /**
     *资金明细
     * @param
     * @param
     * @return
     */
    @ResponseBody
    @GetMapping("moneyInfo")
    public  CommonResult moneyInfo(String employeeId){
         List<MoneyInfo> moneyInfos=new LinkedList<>();
         List<WxProductOrder> List=productOrderMapper.selectWxProductByUserId(employeeId);
        System.out.println("list:数据库中的list"+List);
         //查询数据库中的wx_product_order
         System.out.println(List);
         //循环遍历List集合
         for(WxProductOrder order: List ){
             //查询orderStatus为1的值
             if(order.getOrderStatus().equals("1")){
                 MoneyInfo moneyInfo=new MoneyInfo();
                 moneyInfo.setOrderStatus(order.getOrderStatus());
                 moneyInfo.setPrice(order.getPayMoney());
                 moneyInfo.setOrderTime(order.getStartTime());
                 System.out.println("开始时间:"+order.getStartTime());
                 moneyInfo.setProductName(order.getWxProduct().getProductName());
                 moneyInfos.add(moneyInfo);
             }
             //查询orderStatus为2的值
             else if(order.getOrderStatus().equals("2")){
                 MoneyInfo moneyInfo=new MoneyInfo();
                 moneyInfo.setOrderStatus(order.getOrderStatus());
                 //moneyInfo.setOrderTime(order.getEndTime());
                 moneyInfo.setOrderTime(order.getEffectiveTime());
                 moneyInfo.setPrice(order.getOrderMoney());
                 moneyInfo.setProductName(order.getWxProduct().getProductName());
                 moneyInfos.add(moneyInfo);
                 //取出赎回订单中的支付信息
                 MoneyInfo moneyInfo1=new MoneyInfo();
                 moneyInfo1.setPrice(order.getPayMoney());
                 moneyInfo1.setOrderTime(order.getStartTime());
                 moneyInfo1.setProductName(order.getWxProduct().getProductName());
                 moneyInfo1.setOrderStatus("1");
                 moneyInfos.add(moneyInfo1);
             }

         }
         System.out.println(moneyInfos);
         return CommonResult.success(moneyInfos);
    }
    @ResponseBody
    @GetMapping("earning")
    public CommonResult earningDetail(String employeeId){
      List<EarningDetail> earningDetails= earningDetailMapper.selectEarningDetail(employeeId);
        for (EarningDetail l:earningDetails ){
            if (l != null){
                 return CommonResult.success(earningDetails);
            }
        }
        return CommonResult.fail(404,"没有该用户");
    }

}
